package com.mall.user.service.impl;

import com.mall.common.response.CommonResponse;
import com.mall.constant.enums.CommonStatus;
import com.mall.constant.strings.SuccessMsg;
import com.mall.entity.TbUserBankCard;
import com.mall.entity.TbUserRecharge;
import com.mall.user.dao.UserRechargeMapper;
import com.mall.user.service.UserBankCardService;
import com.mall.user.service.UserRechargeService;
import com.mall.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class UserRechargeServiceImpl implements UserRechargeService {

    @Autowired
    private UserRechargeMapper userRechargeMapper;

    @Autowired
    private UserBankCardService userBankCardService;

    @Autowired
    private UserService userService;

    /**
     * 充值成功之后需要修改银行卡余额以及用户总充值金额
     * @param userRecharge
     */
    @Override
    public CommonResponse userRecharge(TbUserRecharge userRecharge) {
        userRecharge.setCreateTime(new Date());
        userRechargeMapper.insert(userRecharge);
        //充值揭露添加成功后更改银行卡余额
        TbUserBankCard userBankCard = new TbUserBankCard();
        userBankCard.setUid(userRecharge.getUid());
        userBankCard.setCardId(userRecharge.getCardId());
        userBankCard.setMoney(userRecharge.getRechargeAmount());
        userBankCardService.updateMoneyByCardId(userBankCard);
        //修改用户累计充值金额
        userService.updateAccumulativeTotalByUid(userRecharge.getUid(),userRecharge.getRechargeAmount());
        return new CommonResponse(CommonStatus.VALID.getCode(), SuccessMsg.SUCCESS_CODE_200);
    }
}
